package com.ma.process.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ma.common.config.exception.MaException;
import com.ma.common.result.Result;
import com.ma.model.process.ProcessTemplate;
import com.ma.process.service.OaProcessTemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/process/processTemplate")
public class OaProcessTemplateController {
    @Autowired
    private OaProcessTemplateService processTemplateService;

    //分页查询审批模板
    @ApiOperation("分页查询审批模板")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,@PathVariable Long limit){
        Page<ProcessTemplate> templatePage = processTemplateService.index(page,limit);
        return templatePage != null ? Result.ok(templatePage) : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "上传流程定义")
    @PostMapping("/uploadProcessDefinition")
    public Result uploadProcessDefinition(MultipartFile file) throws FileNotFoundException {
        //获取classes路径
        String path = new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath();
        String filename = file.getOriginalFilename();
        //上传目录
        File tempFile = new File(path + "/processes");
        if(!tempFile.exists()){
            tempFile.mkdirs();
        }
        //创建空文件用于写入文件
        File imageFile = new File(path + "/processes" + filename);
        //保存文件到本地
        try {
            file.transferTo(imageFile);
        } catch (IOException e) {
            throw new MaException(201,"文件上传失败，请重试");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("processDefinitionPath", "processes/" + filename); //文件路径
        map.put("processDefinitionKey", filename.substring(0, filename.lastIndexOf("."))); //文件名称作为后面流程key
        return Result.ok(map);
    }

    @PreAuthorize("hasAuthority('bnt.processTemplate.publish')")
    @ApiOperation(value = "发布")
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable Long id) {
        processTemplateService.publish(id);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return Result.ok(processTemplate);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result save(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.save(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public Result updateById(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.updateById(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        processTemplateService.removeById(id);
        return Result.ok();
    }
}
